package soul.mq;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.UUID;

public class MessageEvent<T> {
        /**
         * 版本号
         */
        private Integer version = 1;
        /**
         * 事件名称
         */
        private String name;

        /**
         * 事件类型
         */
        private Integer type;

        /**
         * 事件ID
         */
        private String uuid;

        /**
         * 消息
         */
        private T message;

        public MessageEvent() {
            this.uuid = UUID.randomUUID().toString();
        }

        public MessageEvent(MessageEventType eventType, T message) {
            this.name = eventType.getName();
            this.type = eventType.getType();
            this.message = message;
            this.uuid = UUID.randomUUID().toString();
        }

        public static<M> M  parseJsonToMsgEvent(String jsonEvent,TypeReference<M> type ){
            ObjectMapper mapper = new ObjectMapper();
            try {
                return mapper.readValue(jsonEvent, type);
            } catch (IOException e) {
               //
            }
            return null;
        }

        public String toJsonString(){
            return JSON.toJSONString(this);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getVersion() {
            return version;
        }

        public void setVersion(Integer version) {
            this.version = version;
        }

        public T getMessage() {
            return message;
        }

        public void setMessage(T message) {
            this.message = message;
        }

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
}
